﻿Imports System.IO
Imports System.Threading

Module Utils

    Public TemplateFile As String
    Public TemplateSW As System.IO.StreamWriter

    Public Enum Template
        Header
        Body
        Footer
    End Enum

    Public Sub main()
        Dim _ret As Boolean = True

        Application.EnableVisualStyles()

        If Not File.Exists(MyConstant.Template.TemplateFileBody) Then
            MsgBox("Missing Body Template File")
            _ret = False
        End If
        If _ret AndAlso Not File.Exists(MyConstant.Template.TemplateFileHeader) Then
            MsgBox("Missing Header Template File")
            _ret = False
        End If
        If _ret AndAlso Not File.Exists(MyConstant.Template.TemplateFileFooter) Then
            MsgBox("Missing Footer Template File")
            _ret = False
        End If

        AddHandler AppDomain.CurrentDomain.UnhandledException, AddressOf OnUnhandledException
        AddHandler Application.ThreadException, AddressOf OnUnhandledException

        If _ret Then
            Dim f As New Main
            f.ShowDialog()
        Else
            Application.Exit()
        End If
    End Sub

    Sub OnUnhandledException(ByVal sender As Object, ByVal e As ThreadExceptionEventArgs)
        MsgBox(e.Exception.Message, MsgBoxStyle.Critical, "Application Error")
    End Sub

    Sub OnUnhandledException(ByVal sender As Object, ByVal e As UnhandledExceptionEventArgs)
        MsgBox(DirectCast(e.ExceptionObject, Exception).Message, MsgBoxStyle.Critical, "Application Error")
    End Sub

    Public Function GetFileSize(ByVal FileName As String)
        Dim res As String = String.Empty
        Try
            Dim fi As FileInfo = New FileInfo(FileName)
            res = StrFormatByteSize(fi.Length)
        Catch ex As Exception
            res = String.Empty
        End Try
        Return res
    End Function

    Public Function TextBetween(ByVal CompleteString As String, ByVal StartString As String, ByVal Endstring As String) As String
        Dim ret As String = String.Empty
        Dim x, y As Integer
        Try
            If Not String.IsNullOrEmpty(CompleteString) Then
                x = CompleteString.IndexOf(StartString)
                y = CompleteString.IndexOf(Endstring, x + StartString.Length)
                x = x + StartString.Length
                ret = Trim(CompleteString.Substring(x, y - x))
            End If
        Catch ex As Exception
            ret = String.Empty
        End Try
        Return ret
    End Function

    Public Function GetFileExtension(ByVal FileName As String)
        Dim res As String = String.Empty
        Try
            Dim fi As FileInfo = New FileInfo(FileName)
            res = fi.Extension
        Catch ex As Exception
            res = String.Empty
        End Try
        Return res
    End Function

    Public Sub FreeMemory()
        System.Diagnostics.Process.GetCurrentProcess.MinWorkingSet = New System.IntPtr(5)
    End Sub

    Public Function GetLocalFilesFromFolder(ByRef dir As String, ByVal Extensions As String) As Dictionary(Of Integer, String)
        Dim FileParts As String()
        'ArrayList to hold the files with the certain extensions
        Dim myFiles As New Dictionary(Of Integer, String)
        Dim fsearch As New FileSearch()

        fsearch.Search(dir, Extensions)

        For Each fn As FileInfo In fsearch.Files
            FileParts = fn.Name.Split(New Char() {"-"c})
            If FileParts IsNot Nothing AndAlso FileParts.Rank >= 0 Then
                Try
                    If Not myFiles.ContainsKey(Integer.Parse(FileParts(0).ToLower.Replace("zagor", "").Replace(".", "").Trim)) Then
                        myFiles.Add(FileParts(0).ToLower.Replace("zagor", "").Replace(".", "").Trim, fn.FullName)
                    End If
                Catch ex As Exception
                End Try
            End If
        Next
        Return myFiles
    End Function

    Public Sub StartTemplate(ByVal path As String, sstart As String, send As String)
        Try
            TemplateFile = path & "\ZagorTemplate " & sstart & " - " & send & ".txt"
            TemplateSW = New System.IO.StreamWriter(TemplateFile, False, encoding:=System.Text.Encoding.GetEncoding("ISO-8859-1"))
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
    End Sub

    Public Sub EndTemplate()
        Try

            TemplateSW.Flush()
            TemplateSW.Close()
            TemplateSW = Nothing
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
    End Sub

    Public Function GetTemplate(ByVal template As Template) As String
        Dim res As String = String.Empty
        Dim _file As String = String.Empty
        Try

            Select Case template
                Case Utils.Template.Body
                    _file = MyConstant.Template.TemplateFileBody
                Case Utils.Template.Footer
                    _file = MyConstant.Template.TemplateFileFooter
                Case Utils.Template.Header
                    _file = MyConstant.Template.TemplateFileHeader
            End Select

            Using reader As StreamReader = New StreamReader(_file, encoding:=System.Text.Encoding.GetEncoding("ISO-8859-1"))
                res = reader.ReadToEnd
            End Using
        Catch ex As Exception
            res = String.Empty
        End Try
        Return res
    End Function
End Module
